﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;

namespace FPSO.HMI.Controls
{
	/// <summary>
	/// UCValve.xaml 的交互逻辑
	/// </summary>
	public partial class UCValve : UserControl
	{
		public UCValve()
		{
			InitializeComponent();
			//stateChangeTimer = new DispatcherTimer();
			//stateChangeTimer.Interval = MillSpan;
			//stateChangeTimer.Tick += StateChangeTimer_Tick;

			//preChangeTime = DateTime.MinValue;
			//preActive = false;
			//preActive = false;
		}

		public DispatcherTimer stateChangeTimer;

		//public static Brush CloseBrush = new SolidColorBrush(Color.FromRgb(0x97, 0x97, 0x97));
		//public static Brush ActiveBrush = new SolidColorBrush(Color.FromRgb(0x5B, 0x8B, 0x00));
		public static Brush NormalBrush = new SolidColorBrush(Colors.Gray);
		public static Brush OpeningBrush = new SolidColorBrush(Colors.LightBlue);
		public static Brush OpenedBrush = new SolidColorBrush(Colors.LimeGreen);
		public static Brush ClosingBrush = new SolidColorBrush(Colors.Yellow);
		public static Brush ClosedBrush = new SolidColorBrush(Colors.Orange);
		public static Brush FaultBrush = new SolidColorBrush(Colors.Red);

		public static Brush ActiveBrush = new SolidColorBrush(Colors.Green);
		public static Brush NormalBGBrush = new SolidColorBrush(Color.FromRgb(0x33, 0xA1, 0xC9));

		public static TimeSpan MillSpan = new TimeSpan(0, 0, 0, 0, 100);
		//public static Brush NormalBgBrush = new SolidColorBrush(Color.FromRgb(0x33, 0xA1, 0xC9));
		//public static Brush AnimationBgBrush = new SolidColorBrush(Color.FromRgb(0xD0, 0x5E, 0x3F));

		private void StateChangeTimer_Tick(object sender, EventArgs e)
		{
			//double seconds = (DateTime.Now - preChangeTime).TotalSeconds;
			//if (seconds > 3)
			//{
                //debug去掉动画
                //arcProgress.Point = new Point(12, 0);
				//ellipseState.Fill = Active ? ActiveBrush : CloseBrush;
				//stateChangeTimer.Stop();
				//StateColor = Color.FromRgb(0x33, 0xA1, 0xC9);
			//}
			//debug去掉动画
			//else
			//{

			//	double angle = 360 * (seconds / 3); //得到角度
			//	double rad = (Math.PI / 180d) * (angle + 270);
			//	double x = 12 * Math.Cos(rad) + 12;
			//	double y = 12 * Math.Sin(rad) + 12;
			//	arcProgress.IsLargeArc = angle > 180d;
			//	arcProgress.Point = new Point(x, y);
			//}
		}

		//      public static readonly DependencyProperty OpenCmdProperty =
		//	DependencyProperty.Register("OpenCmd", typeof(bool), typeof(UCValve), new PropertyMetadata(false, OpenCmdPropertyChange));
		//public bool OpenCmd
		//{
		//	get { return (bool)GetValue(OpenCmdProperty); }
		//	set { SetValue(OpenCmdProperty, value); }
		//}

		//public static readonly DependencyProperty CloseCmdProperty =
		//	DependencyProperty.Register("CloseCmd", typeof(bool), typeof(UCValve), new PropertyMetadata(false, CloseCmdPropertyChange));
		//public bool CloseCmd
		//{
		//	get { return (bool)GetValue(CloseCmdProperty); }
		//	set { SetValue(CloseCmdProperty, value); }
		//}

		//public static readonly DependencyProperty StatusOpenedProperty =
		//	DependencyProperty.Register("StatusOpened", typeof(bool), typeof(UCValve), new PropertyMetadata(false, StatusOpenedPropertyChange));
		//public bool StatusOpened
		//{
		//	get { return (bool)GetValue(StatusOpenedProperty); }
		//	set { SetValue(StatusOpenedProperty, value); }
		//}

		//public static readonly DependencyProperty StatusClosedProperty =
		//	DependencyProperty.Register("StatusClosed", typeof(bool), typeof(UCValve), new PropertyMetadata(false, StatusClosedPropertyChange));
		//public bool StatusClosed
		//{
		//	get { return (bool)GetValue(StatusClosedProperty); }
		//	set { SetValue(StatusClosedProperty, value); }
		//}

		//public static readonly DependencyProperty StatusFaultProperty =
		//	DependencyProperty.Register("StatusFault", typeof(bool), typeof(UCValve), new PropertyMetadata(false, StatusFaultPropertyChange));
		//public bool StatusFault
		//{
		//	get { return (bool)GetValue(StatusFaultProperty); }
		//	set { SetValue(StatusFaultProperty, value); }
		//}

		/// <summary>
		/// 状态色
		/// </summary>
		//public static readonly DependencyProperty OpenColorProperty =
		//	DependencyProperty.Register("OpenColor", typeof(Color), typeof(UCValve), new PropertyMetadata(Colors.Green));
		//public Color OpenColor
		//{
		//	get { return (Color)GetValue(OpenColorProperty); }
		//	set { SetValue(OpenColorProperty, value); }
		//}

		//public static readonly DependencyProperty CloseColorProperty =
		//DependencyProperty.Register("CloseColor", typeof(Color), typeof(UCValve), new PropertyMetadata(Colors.DarkGray));
		//public Color CloseColor
		//{
		//	get { return (Color)GetValue(CloseColorProperty); }
		//	set { SetValue(CloseColorProperty, value); }
		//}

		//public static readonly DependencyProperty FaultColorProperty =
		//DependencyProperty.Register("FaultColor", typeof(Color), typeof(UCValve), new PropertyMetadata(Colors.Red));
		//public Color FaultColor
		//{
		//	get { return (Color)GetValue(FaultColorProperty); }
		//	set { SetValue(FaultColorProperty, value); }
		//}

		//public static readonly DependencyProperty ActiveProperty =
		//          DependencyProperty.Register("Active", typeof(bool), typeof(UCValve), new PropertyMetadata(false, ActivePropertyChange));
		//      public bool Active
		//      {
		//          get
		//	{
		//		//int res = (int)GetValue(ActiveProperty);
		//		bool res = (bool)GetValue(ActiveProperty);
		//		return res;
		//	}
		//          set 
		//	{ 
		//		SetValue(ActiveProperty, value);
		//	}
		//      }

		public static readonly DependencyProperty ActiveProperty =
				  DependencyProperty.Register("Active", typeof(bool), typeof(UCValve), new PropertyMetadata(false));
		public bool Active
		{
			get
			{
				bool res = (bool)GetValue(ActiveProperty);
				return res;
			}
			set
			{
				SetValue(ActiveProperty, value);
			}
		}

		public static readonly DependencyProperty ActiveStatusProperty =
		  DependencyProperty.Register("ActiveStatus", typeof(int), typeof(UCValve), new PropertyMetadata(3, new PropertyChangedCallback(ActiveStatusPropertyChange)));
		public int ActiveStatus
		{
			get
			{
				int res = (int)GetValue(ActiveStatusProperty);
				return res;
			}
			set
			{
				SetValue(ActiveStatusProperty, value);
			}
		}

		/// <summary>
		/// 状态色
		/// </summary>
		public static readonly DependencyProperty StateColorProperty =
			DependencyProperty.Register("StateColor", typeof(Color), typeof(UCValve), new PropertyMetadata(Color.FromRgb(0x33, 0xA1, 0xC9)));
		public Color StateColor
		{
			get
			{
				return (Color)GetValue(StateColorProperty);
			}
			set
			{
				SetValue(StateColorProperty, value);
			}
		}

		public static readonly DependencyProperty ValveAngleProperty =
		DependencyProperty.Register("ValveAngle", typeof(int), typeof(UCValve), new PropertyMetadata(0));
		public int ValveAngle
		{
			get { return (int)GetValue(ValveAngleProperty); }
			set { SetValue(ValveAngleProperty, value); }
		}

		//private bool preActive;
		//private DateTime preChangeTime;
		//private static void ActivePropertyChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
		//{
		//	UCValve v = d as UCValve;
		//	v.ActiveChange();
		//}

		private static void ActiveStatusPropertyChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			UCValve v = d as UCValve;
			v.ActiveStatusChange();
		}

		//private void ActiveChange()
		//{
		//	//DEBUG 这里暂时封掉动画
		//	if (preActive != Active)
		//	{
		//		//if (!stateChangeTimer.IsEnabled)
		//		//{
		//		//	stateChangeTimer.Start();
		//		//}
  //              //debug去掉动画，改为红色。NormalBrush => FaultBrush
  //              ellipseState.Fill = FaultBrush/*preActive ? CloseBrush : ActiveBrush*/;
		//		pathProgress.Fill = Active ? ActiveBrush : CloseBrush;
  //              //debug去掉动画
  //              //arcProgress.Point = new Point(12, 0);
		//	}

		//	preChangeTime = DateTime.Now;
		//	preActive = Active;
		//}
		private void ActiveStatusChange()
		{
			if (ActiveStatus == 0) //fault
			{
				ellipseState.Fill = FaultBrush;
				//StateColor = Colors.Red;
			}
			else if (ActiveStatus == 1) //opened
			{
				ellipseState.Fill = OpenedBrush;
				//StateColor = Colors.Green;
			}
			else if(ActiveStatus == 2) //opening
			{
				ellipseState.Fill = OpeningBrush;
			}
			else if (ActiveStatus == 3) //closed
			{
				ellipseState.Fill = ClosedBrush;
				//StateColor = Colors.Orange;
			}
			else if (ActiveStatus == 4) //closing
			{
				ellipseState.Fill = ClosingBrush;
				//StateColor = Colors.Orange;
			}
			else
			{
				ellipseState.Fill = NormalBrush;
				//StateColor = Color.FromRgb(0x33, 0xA1, 0xC9);
			}
		}
	}
}
